﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SCMS.UI.Models;
using SCMS.CoreBusinessLogic.ActionFilters;
using SCMS.CoreBusinessLogic.Web;
using SCMS.CoreBusinessLogic.Security;
using SCMS.Model;

namespace SCMS.UI.Controllers
{
    [MyException]
    public class PayrollController : PortalBaseController
    {
        public PayrollController(IPermissionService permissionService, IUserContext userContext) : base(userContext, permissionService) { }
        //
        // GET: /Payroll/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult ListView()
        {
            using (var db = new SCMSEntities())
            {
                return View("ListView", db.PayrollItems.Where(p => p.CountryProgramId == countryProg.Id).OrderBy(p => p.Name).ToList());    
            }
        }

        public ActionResult Create()
        {
            //var model = new Payroll() { EntityPayroll = new Model.PayrollItem() };
            return View("CreateEdit", new PayrollItem());
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult CreateEdit(PayrollItem entity)
        {
            using (var db = new SCMSEntities())
            {
                entity.CountryProgramId = countryProg.Id;
                if (entity.Id.Equals(Guid.Empty))
                {
                    entity.Id = Guid.NewGuid();
                    db.PayrollItems.AddObject(entity);
                }
                else
                {
                    db.PayrollItems.Attach(entity);
                    db.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Modified);
                }
                db.SaveChanges();
            }
            return ListView();
        }

        //
        // GET: /PayrollItem/Edit/5

        public ActionResult Edit(Guid id)
        {
            using (var db = new SCMSEntities())
            {
                return View("CreateEdit", db.PayrollItems.FirstOrDefault(p => p.Id == id));
            }
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Delete(Guid id)
        {
            using (var db = new SCMSEntities())
            {
                db.DeleteObject(db.PayrollItems.FirstOrDefault(p => p.Id == id));
                db.SaveChanges();
            }
            return ListView();
        }
    }
}
